package util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class SimProperties {

	public final static String DEFAULT_PROPERTIES_FILE = "../simulation.properties";

	private static SimProperties instance = new SimProperties();
	private Properties properties;

	private SimProperties() {
		properties = new Properties();
		initProps();
	}

	public static SimProperties getInstance() {
//		if (instance == null) {
//			instance = new SimProperties();
//			init();
//		}
		return instance;
	}

	public void loadProperties(String name) {
		InputStream in = null;
		try {
			// "../simulation.properties"
			in = this.getClass().getResourceAsStream(name);

			properties.load(in);
		} catch (IOException | NullPointerException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException | NullPointerException e) {
				e.printStackTrace();
			}
		}

	}

	public int getIntProperty(String name) {
		String prop = properties.getProperty(name);
		int retVal = -1;
		if (prop != null) {
			retVal = Integer.parseInt(prop);
		}
		return retVal;
	}

	public float getFloatProperty(String name) {
		String prop = properties.getProperty(name);
		float retVal = -1;
		if (prop != null) {
			retVal = Float.parseFloat(prop);
		}
		return retVal;
	}
	
	public String getStringProperty(String name) {
		String prop = properties.getProperty(name);
		String retVal = "";
		if (prop != null) {
			retVal = prop;
		}
		return retVal;
	}
	
	public boolean getBooleanProperty(String name){
		String prop = properties.getProperty(name);
		boolean retVal = false; // faux par défaut
		if (prop != null) {
			retVal = Boolean.parseBoolean(prop);
		}
		return retVal;
	}
	
	private void initProps(){
		properties.clear();
		loadProperties(DEFAULT_PROPERTIES_FILE);
	}

	public static void init() {
		instance.properties.clear();
		instance.loadProperties(DEFAULT_PROPERTIES_FILE);
	}
	
	public List<String> tourveProprietesQuiCommencePar(String debut){
		List<String> retList = new LinkedList<>();
		
		for(String propName: properties.stringPropertyNames()){
			if(propName.startsWith(debut)){
				retList.add(propName);
			}
		}
		
		return retList;
	}
	
	/**
	 * 
	 * @param inProps
	 */
	public void setCommande(Properties inProps) {
		// la fonction put remplace les propriétés déjà présentes
		properties.putAll(inProps); 
		
	}

}
